package com.sunny.ynhz.ui.daily.news;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.RecentNewsAdapter;
import com.sunny.ynhz.adapter.SpaceItemDecoration;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.RecentNewsBean;
import com.sunny.ynhz.ui.ProblemDeilActivity;
import com.sunny.ynhz.ui.base.BaseFragment;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017-4-11.
 */
public class RecentNewsFragment extends BaseFragment {


    @BindView(R.id.recent_news_recyclerview)
    RecyclerView recentNewsRecyclerview;
    private LinkedList<RecentNewsBean.DataBean> dataBeanLinkedList;
    private RecentNewsAdapter recentNewsAdapter;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_recent_news;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
    }

    @Override
    protected void init() {
        super.init();

        dataBeanLinkedList = new LinkedList<>();
        recentNewsAdapter = new RecentNewsAdapter(dataBeanLinkedList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        recentNewsRecyclerview.setLayoutManager(linearLayoutManager);
        recentNewsRecyclerview.addItemDecoration(new SpaceItemDecoration(30));
        recentNewsRecyclerview.setAdapter(recentNewsAdapter);
        getData();
    }

    private void getData() {
        HttpManager.getInstance()
                .getRecentNews(3)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiSubscriber(activity, new ApiSubscriber.HttpOnResultListener<RecentNewsBean>() {
                    @Override
                    public void onNext(RecentNewsBean jsonObject) {
                        recentNewsRecyclerview.setVisibility(View.VISIBLE);
                        dataBeanLinkedList.clear();
                        dataBeanLinkedList.addAll(jsonObject.getData());
                        recentNewsAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onstart() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick
    public void onClick() {
        doActivity(ProblemDeilActivity.class);
    }
}
